<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改消费记录')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-record-edit" th:object="${consumptionRecord}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">入住房间：</label>
                    <div class="col-sm-8">
                        <select name="roomId" class="form-control" required>
                            <option th:each="room:${rooms}" th:value="${room.id}"
                                    th:text="${room.number}" th:selected="${room.id == consumptionRecord.roomId}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">客人：</label>
                    <div class="col-sm-8">
                        <select name="custmerId" class="form-control" required>
                            <option th:each="guest:${guests}" th:value="${guest.id}"
                                    th:text="${guest.name}" th:selected="${guest.id == consumptionRecord.custmerId}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">服务项目：</label>
                    <div class="col-sm-8">
                        <select name="serviceItemId" class="form-control" required onchange="updateItemPrice(this.value)">
                            <option th:each="serviceItem:${serviceItems}" th:value="${serviceItem.id}"
                                    th:text="${serviceItem.name}" th:selected="${serviceItem.id == consumptionRecord.serviceItemId}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <input name="itemPrice" id="itemPrice" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">消费数量：</label>
                    <div class="col-sm-8">
                        <input name="quantity" th:field="*{quantity}" class="form-control" type="text" required oninput="calculateTotalPrice()">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">消费总金额：</label>
                    <div class="col-sm-8">
                        <input name="totalPrice" th:field="*{totalPrice}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/consumption";
        $("#form-record-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-record-edit').serialize());
            }
        }

        $("input[name='createdTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='updatedTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        document.addEventListener('DOMContentLoaded', function () {
            var serviceItemSelect = document.getElementsByName('serviceItemId')[0];

            var selectedIndex = serviceItemSelect.selectedIndex;
            console.log("选中的索引：", selectedIndex);
            var selectedValue = serviceItemSelect.options[selectedIndex].value;
            console.log("选中的值：", selectedValue);
            updateItemPrice(selectedValue);
        });

        // 当服务项目下拉列表更改时更新 itemPrice
        function updateItemPrice(selectedServiceItemId) {
            var services = [[${serviceItems}]];
            for (var i = 0; i < services.length; i++) {
                if (Number(services[i].id) === Number(selectedServiceItemId)) {
                    $("input[name='itemPrice']").val(services[i].price);
                    break;
                }
            }
        }

        // 计算消费总金额
        function calculateTotalPrice() {
            console.log("计算消费总金额...");
            var itemPrice = parseFloat($("input[name='itemPrice']").val());
            console.log("项目价格：", itemPrice);
            var quantity = parseInt($("input[name='quantity']").val());
            console.log("数量：", quantity);
            if (!isNaN(itemPrice) &&!isNaN(quantity)) {
                var totalPrice = itemPrice * quantity;
                $("input[name='totalPrice']").val(totalPrice);
            }
        }
    </script>
</body>
</html>